package com.ssyc.gsk_tk.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.ssyc.common.adapter.VpCommonAdapter;
import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.view.vp.SuperViewPager;
import com.ssyc.gsk_tk.R;
import com.ssyc.gsk_tk.bean.AnswerInfo;
import com.ssyc.gsk_tk.bean.OverQuestion;
import com.ssyc.gsk_tk.bean.QuestionInfo;
import com.ssyc.gsk_tk.bean.ReadInfo;
import com.ssyc.gsk_tk.bean.ReadLvInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 完形填空
 * Created by Administrator on 2018/9/3 0003.
 */

public class TkClozeTestFragment extends LazyBaseFragment implements ViewPager.OnPageChangeListener {

    //页面对象
    private QuestionInfo.ListBean.ItemBean info;

    //已完成的页面对象
    private OverQuestion.ListBean.ItemBean overinfo;

    //页面的json数据
    private String jsonData;
    //标题
    private TextView tvTitle;
    //题目内容
    private TextView tvContent;
    private SuperViewPager vp;
    //vp的页面指示器
    private TextView tvNum;
    private ReadInfo readInfo;

    //填充ViewPager的Framgent集合
    private List<Fragment> mFragments;

    //vp的适配器
    private VpCommonAdapter adapter;

    private String[] letters = {"A", "B", "C", "D", "E", "F"};

    //以完成状态下对应的所有题型的解析和自己选择的状态
    private String[] analysis;

    private String[] chooseAnswers;

    //所有小题选项的正确率
    private List<OverQuestion.ListBean.ItemBean.ProbabilityBean> probability;

    public static final TkClozeTestFragment newInstance(QuestionInfo.ListBean.ItemBean item, OverQuestion.ListBean.ItemBean overData) {
        TkClozeTestFragment teacherGoldFragment = new TkClozeTestFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", item);
        bundle.putSerializable("overdata", overData);
        teacherGoldFragment.setArguments(bundle);
        return teacherGoldFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tk_cloze_test;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);
        //显示内容
        showContent();

        //获取页面对象
        initIntent();

        //初始化视图
        initView(view);

        //初始化页面数据
        initData();
    }


    /**
     * 获取页面对象
     */
    private void initIntent() {
        //未完成的数据对象
        info = (QuestionInfo.ListBean.ItemBean) getArguments().getSerializable("data");

        //已完成的数据对象
        overinfo = (OverQuestion.ListBean.ItemBean) getArguments().getSerializable("overdata");

        if (info != null) {
            //如果是未完成则解析info
            jsonData = info.getContent();
            if (!TextUtils.isEmpty(jsonData)) {
                readInfo = GsonUtil.jsonToBean(jsonData, ReadInfo.class);
            }
        } else if (overinfo != null) {

            //获取解析和自己填写的答案的集合
            //获取所有题目的解析集合使用￠切割
            analysis = overinfo.getAnalysis().split("￠");



            //获取所有的自己作答的选项集合
            chooseAnswers = overinfo.getOption_content().split("￠");

            //如果是已完成则解析overinfo
            jsonData = overinfo.getContent();
            if (!TextUtils.isEmpty(jsonData)) {
                readInfo = GsonUtil.jsonToBean(jsonData, ReadInfo.class);
            }
            probability = overinfo.getProbability();
        }
    }

    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        tvTitle = view.findViewById(R.id.tv_title);
        tvContent = view.findViewById(R.id.tv_content);
        vp = view.findViewById(R.id.vp);
        vp.addOnPageChangeListener(this);
        tvNum = view.findViewById(R.id.tv_num);
    }


    /**
     * 初始化页面的数据,包含阅读的内容,以及提供选择的
     */
    private void initData() {

        if (readInfo != null) {
            //获取题干内容
            String questionContent = readInfo.getContent();
            tvContent.setText(questionContent);

            //获取题目选项
            List<ReadInfo.ListBean> questions = readInfo.getList();

            //创建底部选择答案的Fragment页面的数据集合
            List<AnswerInfo> answerInfos = new ArrayList<>();

            for (int i = 0; i < questions.size(); i++) {

                AnswerInfo info = new AnswerInfo();

                //以完成的状态写设置该小题的解析和该小题自己选择的答案
                if (overinfo != null) {
                    info.explain = analysis[i];
                    Log.i("test",analysis[i]);
                    info.chooseAnswer = chooseAnswers[i];
                }

                //设置正确答案
                info.rightAnswer = questions.get(i).getR();

                //选择答案的集合
                List<ReadLvInfo> readInfos = new ArrayList<>();

                String A = questions.get(i).getA();
                String B = questions.get(i).getB();
                String C = questions.get(i).getC();
                String D = questions.get(i).getD();
                String E = questions.get(i).getE();
                String F = questions.get(i).getF();

                //设置选择的答案选项
                if (putAnswer(0, A) != null) {
                    readInfos.add(putAnswer(0, A));
                }
                if (putAnswer(1, B) != null) {
                    readInfos.add(putAnswer(1, B));
                }
                if (putAnswer(2, C) != null) {
                    readInfos.add(putAnswer(2, C));
                }
                if (putAnswer(3, D) != null) {
                    readInfos.add(putAnswer(3, D));
                }
                if (putAnswer(4, E) != null) {
                    readInfos.add(putAnswer(4, E));
                }
                if (putAnswer(5, F) != null) {
                    readInfos.add(putAnswer(5, F));
                }

                info.readLvInfos = readInfos;

                OverQuestion.ListBean.ItemBean.ProbabilityBean probabilityBean = null;

                if (probability != null && probability.size() != 0) {
                    probabilityBean = probability.get(i);
                }

                if (overinfo != null) {

                    for (int j = 0; j < readInfos.size(); j++) {

                        if (null == probabilityBean) {
                            readInfos.get(j).probability = "0.0%";
                        } else {
                            if (j == 0) {
                                if (!TextUtils.isEmpty(probabilityBean.getA())) {
                                    readInfos.get(j).probability = probabilityBean.getA();
                                } else {
                                    readInfos.get(j).probability = "0.0%";
                                }
                            } else if (j == 1) {
                                if (!TextUtils.isEmpty(probabilityBean.getB())) {
                                    info.readLvInfos.get(j).probability = probabilityBean.getB();
                                } else {
                                    info.readLvInfos.get(j).probability = "0.0%";
                                }
                            } else if (j == 2) {
                                if (!TextUtils.isEmpty(probabilityBean.getC())) {
                                    info.readLvInfos.get(j).probability = probabilityBean.getC();
                                } else {
                                    info.readLvInfos.get(j).probability = "0.0%";
                                }
                            } else if (j == 3) {
                                if (!TextUtils.isEmpty(probabilityBean.getD())) {
                                    info.readLvInfos.get(j).probability = probabilityBean.getD();
                                } else {
                                    info.readLvInfos.get(j).probability = "0.0%";
                                }
                            }
                        }
                    }
                }
//                if (overinfo != null) {
//                    if (probability != null && probability.size() != 0) {
//                        for (int j = 0; j < probability.size(); j++) {
//                            if (!TextUtils.isEmpty(probability.get(j).getA())) {
//                                info.readLvInfos.get(0).probability = probability.get(j).getA();
//                            }
//                            if (!TextUtils.isEmpty(probability.get(j).getB())) {
//                                info.readLvInfos.get(1).probability = probability.get(j).getB();
//                            }
//                            if (!TextUtils.isEmpty(probability.get(j).getC())) {
//                                info.readLvInfos.get(2).probability = probability.get(j).getC();
//                            }
//                            if (!TextUtils.isEmpty(probability.get(j).getD())) {
//                                info.readLvInfos.get(3).probability = probability.get(j).getD();
//                            }
//                            if (!TextUtils.isEmpty(probability.get(j).getE())) {
//                                info.readLvInfos.get(4).probability = probability.get(j).getE();
//                            }
//                            if (!TextUtils.isEmpty(probability.get(j).getF())) {
//                                info.readLvInfos.get(5).probability = probability.get(j).getF();
//                            }
//                        }
//                    }
//                }
                answerInfos.add(info);
            }

            //根据获取的answerInfos来创建填充ViewPager的Fragment
            mFragments = new ArrayList<>();
            if (answerInfos.size() != 0) {
                for (int i = 0; i < answerInfos.size(); i++) {
                    //这里将是否是以完成和未完成的标识传递0代表未完成,1代表已完成
                    if (info != null) {
                        mFragments.add(TkClozeTestChooseAnswerFragment.newInstance(answerInfos.get(i), i, 0));
                    } else if (overinfo != null) {
                        mFragments.add(TkClozeTestChooseAnswerFragment.newInstance(answerInfos.get(i), i, 1));
                    }
                }

                //设置底部的VP的指示器数字
                tvNum.setText("1/" + mFragments.size());

                //设置ViewPager的数据
                adapter = new VpCommonAdapter(getChildFragmentManager(), mFragments);
                vp.setAdapter(adapter);
                vp.setOffscreenPageLimit(mFragments.size());
            }
        }
    }

    /**
     * 返回答案是 "A":"Smart. ",拆分
     * A到F对应的字母索引
     *
     * @param answer 答案选择
     */
    public ReadLvInfo putAnswer(int index, String answer) {

        if (TextUtils.isEmpty(answer)) {
            return null;
        } else {
            ReadLvInfo readLvInfo = new ReadLvInfo();
            //先用:切割
            readLvInfo.isSelected = false;
            readLvInfo.questionIndex = letters[index];
            readLvInfo.questionContent = answer;
            return readLvInfo;
        }
    }


    /**
     * ViewPager的滚动监听事件
     *
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tvNum.setText((position + 1) + "/" + mFragments.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
